/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trinomialtree.valuegenerator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author pl
 */
public class ValueGenerator {

    final String RATE_INPUT = "C:\\Users\\eva\\Documents\\NetBeansProjects\\TrinomialTree\\input\\";

    public ValueGenerator() {
    }

    public void generateMonthly(String inputfile, String outputfile) throws Exception {

        // Öffne das input file
        BufferedReader input = new BufferedReader(new FileReader(this.RATE_INPUT + inputfile));

        String line = null;
        List<String> yearRates = new ArrayList<String>();

        while ((line = input.readLine()) != null) {

            yearRates.add(line);

        }
        input.close();




        //Berechne die monatlichen Raten
        List<String> monthlyRates = new ArrayList<String>();
        Double previousValue = 0d;
        int i = 0;
        for (String s : yearRates) {

            Double currentValue = new Double(s.replaceAll(",", "."));

            if (i != 0) {

                //Nimm den aktuellen Wert und ziehe davon den Vorgängerwert ab
                Double delta = currentValue - previousValue;
                Double monthlyAdd = delta / 12;

                //Ausnahme 1. Werte
                if (i != 1) {
                    monthlyRates.add(previousValue.toString());
                }

                Double temp = previousValue;
                for (int j = 0; j < 11; j++) {
                    temp = temp + monthlyAdd;
                    monthlyRates.add(temp.toString());
                }

                //monthlyRates.add(currentValue.toString());
            }
            previousValue = new Double(s.replaceAll(",", "."));
            i++;

            if (i == yearRates.size()) {
                monthlyRates.add(currentValue.toString());
            }
        }

        Writer output = new BufferedWriter(new FileWriter(this.RATE_INPUT + outputfile));

        for (String s : monthlyRates) {
            output.write(s + "\n");
        }
        output.flush();
    }

    public void generateWeekly(String inputfile, String outputfile) throws Exception {

        // Öffne das input file
        BufferedReader input = new BufferedReader(new FileReader(this.RATE_INPUT + inputfile));

        String line = null;
        List<String> yearRates = new ArrayList<String>();

        while ((line = input.readLine()) != null) {


            yearRates.add(line);

        }
        input.close();




        //Berechne die monatlichen Raten
        List<String> monthlyRates = new ArrayList<String>();
        Double previousValue = 0d;
        int i = 0;
        for (String s : yearRates) {

            Double currentValue = new Double(s.replaceAll(",", "."));

            if (i != 0) {

                //Nimm den aktuellen Wert und ziehe davon den Vorgängerwert ab
                Double delta = currentValue - previousValue;
                Double monthlyAdd = delta / 52;

                //Ausnahme 1. Werte
                if (i != 1) {
                    monthlyRates.add(previousValue.toString());
                }

                Double temp = previousValue;
                for (int j = 0; j < 51; j++) {
                    temp = temp + monthlyAdd;
                    monthlyRates.add(temp.toString());
                }

                //monthlyRates.add(currentValue.toString());
            }
            previousValue = new Double(s.replaceAll(",", "."));
            i++;

            if (i == yearRates.size()) {
                monthlyRates.add(currentValue.toString());
            }
        }

        Writer output = new BufferedWriter(new FileWriter(this.RATE_INPUT + outputfile));

        for (String s : monthlyRates) {
            output.write(s + "\n");
        }
        output.flush();
    }

    public void generateYearly(String inputfile, String outputfile) throws Exception {

        // Öffne das input file
        BufferedReader input = new BufferedReader(new FileReader(this.RATE_INPUT + inputfile));

        String line = null;
        List<String> yearRates = new ArrayList<String>();

        int i = 0;

        while ((line = input.readLine()) != null) {



            //Ignore the first entry
            if (i != 0) {
                yearRates.add(line);
            }


            i++;
        }
        input.close();




        Writer output = new BufferedWriter(new FileWriter(this.RATE_INPUT + outputfile));

        for (String s : yearRates) {
            output.write(s + "\n");
        }
        output.flush();
    }

    public static void main(String args[]) {

        try {

            ValueGenerator generator = new ValueGenerator();

            //Generate the yearly EURIBOR values

            //Generate the yearly EURIBOR values
            generator.generateYearly("EURIBOR_yearly_1999_2010_INPUT.csv", "EURIBOR_yearly_1999_2010.csv");

            //Generate the yearly EURIBOR values
            generator.generateYearly("EURIBOR_yearly_2003_2012_INPUT.csv", "EURIBOR_yearly_2003_2012.csv");

            //Generate the yearly EURIBOR values
            generator.generateYearly("EURIBOR_yearly_1998_INPUT.csv", "EURIBOR_yearly_1998.csv");

            //Generate the yearly LIBOR values
            generator.generateYearly("LIBOR_yearly_1999_2010_INPUT.csv", "LIBOR_yearly_1999_2010.csv");





            //Generate the monthly EURIBOR values
            generator.generateMonthly("EURIBOR_yearly_1999_2010_INPUT.csv", "EURIBOR_monthly_1999_2010.csv");

            //Generate the monthly EURIBOR values
            generator.generateMonthly("EURIBOR_yearly_2003_2012_INPUT.csv", "EURIBOR_monthly_2003_2012.csv");

            //Generate the monthly EURIBOR values
            generator.generateMonthly("EURIBOR_yearly_1998_INPUT.csv", "EURIBOR_monthly_1998.csv");

            //Generate the monthly LIBOR values
            generator.generateMonthly("LIBOR_yearly_1999_2010_INPUT.csv", "LIBOR_monthly_1999_2010.csv");


            //Generate the weekly EURIBOR values
            generator.generateWeekly("EURIBOR_yearly_1999_2010_INPUT.csv", "EURIBOR_weekly_1999_2010.csv");

            //Generate the monthly EURIBOR values
            generator.generateWeekly("EURIBOR_yearly_2003_2012_INPUT.csv", "EURIBOR_weekly_2003_2012.csv");

            //Generate the monthly EURIBOR values
            generator.generateWeekly("EURIBOR_yearly_1998_INPUT.csv", "EURIBOR_weekly_1998.csv");

            //Generate the weekly LIBOR values
            generator.generateWeekly("LIBOR_yearly_1999_2010_INPUT.csv", "LIBOR_weekly_1999_2010.csv");


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void out(String s) {
        System.out.println(s);
    }
}
